package com.ihighest.app.infox.activity;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.MyAskAndAnswerAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;
import com.ihighest.app.infox.tools.CheckNetWork;

/**
 * 我的问答
 * 
 * @author lkx Created at 2015-11-4 下午6:30:31
 * @version 1.0
 */
public class MyQuestionAndAnswerActivity extends BaseActivity {
	private ActivityTitle title;
	private PullToRefreshListView collectionListview;
	private MyAskAndAnswerAdapter adapter;
	private String titleName;

	// private NewListViewAdapter melistviewadapter;
	JSONArray jsonArray;
	int page = 1;
	long longtime = 0;
	private TextView emptyview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_mycollection);
		titleName = getIntent().getStringExtra("title");

		initView();
		// initData();
	}

	// /**
	// * 初始化数据
	// *
	// *
	// * @author lkx Created at 2015-11-4 上午11:56:07
	// */
	// private void initData() {
	// ApiParam param = ApiParam.create();
	// param.addParam("page", page);
	// Api.get("me/issue", param, new
	// ApiCallback(MyQuestionAndAnswerActivity.this, true) {
	//
	// @Override
	// public void handleSuccess(ApiResult result) {
	// // TODO Auto-generated method stub
	//
	// }
	//
	// @Override
	// public void handleFailure(Exception e, String msg) {
	// // TODO Auto-generated method stub
	//
	// }
	// }, MyQuestionAndAnswerActivity.this);
	//
	// }

	/**
	 * 初始化绑定
	 * 
	 * 
	 * @author lkx Created at 2015-10-13 上午10:12:02
	 */
	@SuppressWarnings("unchecked")
	private void initView() {
		title = (ActivityTitle) findViewById(R.id.mActivityTitle);
		title.setTitle(titleName);
		title.setViewVersible(title.titleBack);
		title.setViewGone(title.titleMsg, title.titleRefresh, title.rightLayout);
		collectionListview = (PullToRefreshListView) findViewById(R.id.minelistView);
		jsonArray = new JSONArray();
		adapter = new MyAskAndAnswerAdapter(MyQuestionAndAnswerActivity.this, jsonArray);
		collectionListview.setAdapter(adapter);

		// melistviewadapter = new NewListViewAdapter(this, jsonArray);
		// collectionListview.setAdapter(melistviewadapter);
		emptyview = new TextView(this);
		emptyview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		emptyview.setText("加载中...");
		emptyview.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		emptyview.setVisibility(View.GONE);
		collectionListview.setEmptyView(emptyview);
		getdata();
		// collectionListview.setOnItemClickListener(new OnItemClickListener() {
		//
		// @Override
		// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
		// long arg3) {
		// arg2 = arg2 - 1;
		// Intent intent = new Intent(MyQuestionAndAnswerActivity.this,
		// QuestionContentActivity.class);
		// Bundle bundle = new Bundle();
		// try {
		// bundle.putString("id",
		// jsonArray.getJSONObject(arg2).getString("id"));
		//
		// } catch (JSONException e) {
		//
		// }
		// intent.putExtras(bundle);
		// startActivity(intent);
		// }
		// });

		collectionListview.setOnRefreshListener(new OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {

				if (CheckNetWork.isConnected(MyQuestionAndAnswerActivity.this)) {
					emptyview.setText("加载中...");
					page = 1;
					longtime = 0;
					getdata();

				} else {
					ToastKit.showShort(MyQuestionAndAnswerActivity.this, "请连接网络");
				}
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {

				if (CheckNetWork.isConnected(MyQuestionAndAnswerActivity.this)) {
					page++;
					getdata();
				} else {
					ToastKit.showShort(MyQuestionAndAnswerActivity.this, "请连接网络");
				}

			}

		});
		
		collectionListview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				arg2=arg2-1;
			Intent	intent=new Intent(MyQuestionAndAnswerActivity.this,QuestionContentActivity.class);
			   try {
				String id=  jsonArray.getJSONObject(arg2).getJSONObject("issue").getString("id");
				
				intent.putExtra("id", id);
				startActivity(intent);
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
				
				
				
			}
		});

	}

	private void getdata() {

		ApiParam param = ApiParam.create().addParam("page", page);
		if (longtime != 0) {
			param.addParam("time", longtime);
		}
		if (TextUtils.isEmpty(SharedPreferencesKit.getString(this, Const.SP_KEY_USER_ID))) {
			emptyview.setText("您尚未登录");
			collectionListview.onRefreshComplete();
		} else {
			Api.get("me/issue", param, new ApiCallback(this, true) {

				@Override
				public void handleSuccess(ApiResult result) {
					if (result.getCode() == 100) {
						longtime = result.getTime();
						if (page == 1) {

							if (result.getData() == null || result.getDatas().length() <= 0) {
								emptyview.setText("问题列表为空");
							}

							jsonArray = result.getDatas();
							adapter.setData(result.getDatas());
						} else {
							if (result.getDatas() == null || result.getDatas().length() <= 0) {
								page--;
							} else {
								try {
									jsonArray = AppKit.joinJSONArray(jsonArray, result.getDatas());
									adapter.appendData(result.getDatas());
								} catch (JSONException e) {
									Log.e(TAG, e.getMessage(), e);
								}
							}
						}
					} else {
						emptyview.setText("加载失败，请稍后刷新");
					}
					collectionListview.onRefreshComplete();

				}

				@Override
				public void handleFailure(Exception e, String msg) {
					collectionListview.onRefreshComplete();
					emptyview.setText("加载失败，请稍后刷新");
				}
			}, this);
		}

	}
}
